//获取应用实例
var app = getApp();
// var util = require('../../../utils/util.js')
// var wxpay = require('../../../utils/pay.js');

// 普通店主
function createNormal() {
  return {
    // 价格
    price : [ '9.9', '399' ],
    // 横幅图片
    banners : [ 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/x/zq/xzq15ia5me7uez01606537533.png' ],
    // 特权
    privileges : [
      {
        title : '产品供应链',
        list : [ '数百个品类', '数万款商品', '数千款热卖', '厂价直供', '自有产品上传' ]
      },
      {
        title : '培训服务',
        list : [ '免费社群培训', '免费引流培训', '免费运营培训' ]
      },
      {
        title : '直播服务',
        list : [ '不限次直播服务', '免费平台主播带货' ]
      },
      {
        title : '运营服务',
        list : [ '开店全程免费指导', '运营活动全程支持', '社群工具免费使用', '专业客服一对一服务' ]
      }
    ],
    // 精选商品
    boutiques : []
  };
}

// 超级店主
function createSuper() {
  return {
    // 价格
    price : [ '399', '990' ],
    // 横幅图片
    banners : [
      'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/0/7z/07zf2lfar7u8ati1605948453.png',
      'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/s/lu/slujte2ab3se9fu1605947895.png'
    ],
    // 特权
    privileges : [
      {
        title : '普通店主权益',
        list : [ '数百个类目数万款商品供应链', '免费店主培训', '不限次直播服务', '一对一客服服务' ]
      },
      {
        title : '团队收益',
        list : [ '直推高级店主奖励', '团队高级店主奖励', '直推店主销售订单返佣', '间推店主销售订单返佣' ]
      }
    ],
    // 精选商品
    boutiques : [
      // 名称，描述，价格，图片地址，宽，高
      {
        title : '无印良品四件套',
        desc : '韩式轻奢北欧风磨毛亲肤拼色',
        money : 399,
        url : 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/w/iy/wiynbsp5imcy58g1605947645.png',
        width : '313rpx',
        height : '225rpx',
        value : '1',
        checked : false
      },
      {
        title : '一惯好礼大红袍',
        desc : '品好茶送好礼  9罐带茶具',
        money : 399,
        url : 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/m/9v/m9vpvy4gn8oxz5y1605948560.png\n',
        width : '235rpx',
        height : '229rpx',
        value : '2',
        checked : false
      },
      {
        title : '欧涞宝西班牙',
        desc : '原装进口橄榄油750ml礼盒',
        money : 399,
        url : 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/4/q0/4q0mwhmwc0iwzrf1605948328.png',
        width : '274rpx',
        height : '211rpx',
        value : '3',
        checked : false
      },
      {
        title : '水星家纺四件套',
        desc : '柔软舒适的全棉面料',
        money : 399,
        url : 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/t/10/t10fhaxyfhabauj1605947866.png',
        width : '323rpx',
        height : '195rpx',
        value : '4',
        checked : false
      },
      {
        title : '康佳迷你电饭煲',
        desc : '高质量外观助力开启厨房新生活',
        money : 399,
        url : 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/t/ny/tnyd1bfz5mb50xd1605948345.png',
        width : '243rpx',
        height : '216rpx',
        value : '5',
        checked : false
      },
      {
        title : '富贵吉祥茶',
        desc : '茗茶名器一应俱全四大茗茶',
        money : 399,
        url : 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/0/tt/0ttvi6hrsdrg0mr1605948535.png',
        width : '296rpx',
        height : '233rpx',
        value : '6',
        checked : false
      },
      {
        title : '法国原装礼盒红酒',
        desc : '原瓶原装两只礼盒装红酒',
        money : 799,
        url : 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/h/v9/hv9wblk1nljpdud1605948431.png',
        width : '223rpx',
        height : '228rpx',
        value : '7',
        checked : false
      },
      {
        title : '古井镇纯粮白酒',
        desc : '浓香型四大美人白酒原浆酒',
        money : 699,
        url : 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/p/41/p41yfx1w1m2ibve1605948577.png',
        width : '278rpx',
        height : '190rpx',
        value : '8',
        checked : false
      },
      {
        title : '杏花村纯粮原浆',
        desc : '杏花村纯粮500ml4瓶礼盒装',
        money : 699,
        url : 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/h/jg/hjgei1ru58lx85s1605948592.png',
        width : '258rpx',
        height : '212rpx',
        value : '9',
        checked : false
      },
      {
        title : '赤霞珠葡萄酒两支',
        desc : '法国佳酿纯真味蕾',
        money : 1288,
        url : 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/t/ge/tge3xnhosk61imu1605948032.png',
        width : '122rpx',
        height : '214rpx',
        value : '10',
        checked : false
      }
    ]
  };
}

Page({

  /**
   * 页面的初始数据
   */
  data : {
    // 复选框样式
    checkbox : {
      default : 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/f/dl/fdlaofvv5t4kg5x1605947938.png',
      checked : 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/8/26/82630aeuso8n0zb1605947960.png'
    },
    // 序号
    numbers : [
      'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/d/m8/dm84n9ppzlqv5o71605948471.png',
      'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/v/nb/vnb2blso4zg2djp1605948486.png',
      'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/0/fv/0fvi30mfa6iwsvq1605948501.png',
      'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/catalog/product/1/tt/1ttm3y9qb80tfym1605952425.png'
    ],
    // 金额
    price : [ 0, 0 ],
    // 横幅图片
    banners : [],
    // 特权
    privileges : [],
    // 精选商品
    boutiques : []
  },

  /**
   * 礼物选择
   */
  giftChoose(e) {
    const checked = e.detail.value;
    const boutiques = this.data.boutiques;
    for ( const boutique of boutiques ) {
      boutique.checked = checked === boutique.value;
    }
    this.setData({ boutiques });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 扫码 - 获取传递过来的用户信息
    let store_id, store_v;
    if ( options.scene ) {
      const scene = decodeURIComponent(options.scene), sceneArr = scene.split('&');
      store_id = sceneArr[0];
      store_v = sceneArr[1];
    } else {
      store_id = options.id;
      store_v = options.v;
    }
    wx.setStorageSync('shareId', store_id);
    this.setData({
      store_id : store_id,
      store_v : store_v
    });

    // 登录
    let login = wx.getStorageSync('isPhone');
    let customer_id = wx.getStorageSync('customer_id');
    if ( !login && (!customer_id || (customer_id && (customer_id.head_url == '' || customer_id.phone == '' || !customer_id.phone || !customer_id.head_url))) ) {
      wx.navigateTo({
        url : '/pages/login/login?phone=true'
      });
    }

    // 页面数据
    // 5 普遍店主 / 7 高级店主
    this.setData(Number(store_v) === 5 ? createNormal() : createSuper());
    // 获取金额
    // this.getMoney( store_v );
  },

  // 获取金额
  getMoney(store_v) {
    var that = this;
    wx.request({
      url : app.globalData.urls + '/order/version-info',
      header : app.getRequestHeader(),
      method : 'POST',
      data : {
        merchant_version : store_v
      },
      success : function (res) {
        if ( res.data.code == 200 ) {
          that.setData({
            moneyInfo : res.data.data
          });
          console.log('----res.data.data', res.data.data);
        }
      }
    });
  },

  // 立即开通
  ImmediatelyOpened() {
    let customer_id = wx.getStorageSync('customer_id');
    const boutique = this.data.boutiques.filter(btq => btq.checked)[0];
    wx.request({
      url : app.globalData.urls + '/order/pay-store',
      header : app.getRequestHeader(),
      method : 'POST',
      data : {
        merchant_version : this.data.store_v,
        recommend_id : this.data.store_id,
        customer_id : customer_id.customer_id,
        gift_id : boutique && boutique.value || 0
      },
      success : function (res) {
        if ( res.data.code == 200 ) {
          // 新的页面支付接口
          // 发起支付
          var jsApiParameters = res.data.data.jsApiParameters;
          wx.requestPayment({
            timeStamp : jsApiParameters.timeStamp,
            nonceStr : jsApiParameters.nonceStr,
            package : jsApiParameters.package, // 'prepay_id=' + res.data.data.prepayId,
            signType : jsApiParameters.signType, //'MD5',
            paySign : jsApiParameters.paySign,
            fail : function (aaa) {
              wx.showToast({ title : '支付失败' });
            },
            success : function () {
              wx.showToast({ title : '支付成功' });
              wx.redirectTo({
                url : '/pages/business/promote-success/promote-success'
              });
            }
          });
        } else {
          wx.showToast({
            title : res.data.message,
            icon : 'none'
          });
        }
      }
    });
  }
});
